from Entity import *


class World(object):
    def __init__(self, engine, gameset=Entity.gameset):
        self.engine = engine
        self.entities = {}
        self.getUnnamedEntityCount = self.getUnnamedEntityCounter().next
        self.entityset = gameset

    def getUnnamedEntityCounter(self):
        i = 0
        while True:
            yield i
            i += 1

    def createEntity(self, type_, name=None, *args, **kwargs):
        if name == None:
            name = "unnamed_entity_" + \
                   str(self.getUnnamedEntityCount())
        entity = type_(self.engine, name, *args, **kwargs)
        #print "Creating entity '" + entity.name + "' of type '" + \
        #      type_.__name__ + "'"
        self.entities[entity.name] = entity
        return entity

    def destroyEntity(self, entity):
        # del self.entities[entity.name] alone doesn't seem to work, so...
        self.entities[entity.name].destroy()
        del self.entities[entity.name]

    def update(self, timestep):
        for entity in self.entities.values():
            entity.update(timestep)

    def draw(self):
        pass
        #for entity in self.entities.values():
        #    entity.draw()
